import CryptoJs from'crypto-js'


// 默认的 KEY 与 iv 如果没有给
    const KEY = CryptoJs.enc.Utf8.parse("63eeac68cf074c8c")
    const IV = CryptoJs.enc.Utf8.parse('63eeac68cf074c8c');
    /* AES加密 :字符串 key iv 返回base64
    */
    export function Encrypt(word, keystr, ivstr) {
        let key = KEY; 
        let iv = IV;
        if (keystr) {
            key = CryptoJs.enc.Utf8.parse(keystr); 
            iv = CryptoJs.enc.Utf8.parse(ivstr);
        }
        let srcs = CryptoJs.enc.Utf8.parse(word);
        var encrypted = CryptoJs.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJs.mode.CBC, padding: CryptoJs.pad.ZeroPadding});
        return CryptoJs.enc.Base64.stringify(encrypted.ciphertext);
    }
            
    // 米冲AES 解密:字符串 key iv 返回base64

    export function Decrypt(word, keystr, ivstr) {
        let key = KEY; 
        let iv = IV; 
        if (keystr) {
            key = CryptoJs.enc.Utf8.parse(keystr); 
            iv = CryptoJs.enc.Utf8.parse(ivstr);
        }
        let base64 = CryptoJs.enc.Base64.parse(word); 
        let src = CryptoJs.enc.Base64.stringify(base64); 
        let decrypt = CryptoJs.AES.decrypt(src, key, { iv: iv, mode: CryptoJs.mode.CBC, padding: CryptoJs.pad.ZeroPadding }); 
        let decryptedstr = decrypt.tostring(CryptoJs.enc.Utf8); 
        return decryptedstr.tostring();
    }